package ftp

import (
	"fmt"
	"gin/model"
	"strconv"
	"strings"
	"time"
)

// 开锁日志
func (*DataParsing) parsingFlagLog(catData []string, ftpInfoId int64) []model.DataFlag {
	var logDate, LogTime, steamId, gameName, gameId, eventType, dataType, flagId string
	var flagSteamId, flagGameName, flagGameId, location, flagLocation string
	var end int
	var dataArr []model.DataFlag

	for _, logTxt := range catData {
		//fmt.Println("原始数据", logTxt)

		parts := strings.Split(logTxt, ":")
		//fmt.Println("分割数据", parts)
		timeParts := strings.Split(parts[0], "-")
		//fmt.Println("分割数据2", timeParts)
		//日期转换  年-月-日
		logDate = strings.Replace(timeParts[0], ".", "-", -1)
		//fmt.Println("转换数据1", catDate)
		//时间转换  时:分:秒
		Time := strings.Replace(timeParts[1], ".", ":", -1)
		//fmt.Println("转换数据2", catTime)
		layout := "2006-01-02 15:04:05"
		// 解析时间字符串
		//fmt.Println("解析时间格式为", catDate+" "+catTime)
		t, err := time.Parse(layout, logDate+" "+Time)
		if err != nil {
			fmt.Println("解析时间格式为", err.Error())
			LogTime = "0"
			//return
		}
		// 转换为 Unix 时间戳（秒）

		LogTime = strconv.FormatInt(t.Unix(), 10)
		logTime, _ := strconv.ParseInt(LogTime, 10, 64)
		//太平洋时间制  + 8 小时
		//logTime = logTime + 3600*8
		t = time.Unix(logTime, 0)
		logDate = t.Format("2006-01-02")

		hour, minute, second := t.Clock()
		Time = fmt.Sprintf("%02d:%02d:%02d", hour, minute, second)

		//开锁类型过滤
		start := strings.Index(logTxt, ": [")
		if start < 0 {
			dataType = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+3:], "]")
			if end < 0 {
				dataType = ""
			} else {
				dataType = logTxt[start+3 : start+3+end]
			}
		}

		if dataType != "LogBaseBuilding" {
			//不是领地旗类型跳过
			continue
		}

		//开锁类型过滤
		start = strings.Index(logTxt, "] [")
		if start < 0 {
			dataType = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+3:], "]")
			if end < 0 {
				dataType = ""
			} else {
				dataType = logTxt[start+3 : start+3+end]
			}
		}

		if dataType != "Flag" {
			//不是领地旗标识跳过
			continue
		}

		//事件类型
		start = strings.Index(logTxt, "ag] ")
		if start < 0 {
			eventType = ""
		} else {
			// 查找第二个单引号的位置
			end = strings.Index(logTxt[start+4:], ".")
			if end < 0 {
				eventType = ""
			} else {
				eventType = logTxt[start+4 : start+4+end]
			}
		}

		steamId = ""
		gameName = ""
		gameId = ""
		flagId = ""
		flagSteamId = ""
		flagGameName = ""
		flagGameId = ""
		location = ""
		flagLocation = ""
		switch eventType {
		case "Created":
			//放置
			steamId, gameName, gameId, flagId, location = flagTypeNoe(logTxt)
			break
		case "Destroyed":
			//拆除
			steamId, gameName, gameId, flagId, location = flagTypeNoe(logTxt)
			break
		case "Overtaken (Left squad/Promoted)":
			steamId, gameName, gameId, flagId, location, flagSteamId, flagGameName, flagGameId = flagTypeTwo(logTxt)
			//转让

			break
		case "Overtaken":

			steamId, gameName, gameId, flagId, location, flagSteamId, flagGameName, flagGameId = flagTypeTwo(logTxt)
			//转让

			break
		case "Overtake canceled":

			steamId, gameName, gameId, flagId, location, flagSteamId, flagGameName, flagGameId, flagLocation = flagTypeTree(logTxt)
			break
			//取消占领
		case "Overtake started":

			steamId, gameName, gameId, flagId, flagSteamId, flagGameName, flagGameId, location, flagLocation = flagTypeFour(logTxt)
			//开始占领
			break

		case "Abandoned on overtake":
			//fmt.Println("原始数据", logTxt)
			//占领覆盖  （前一个占领了 后一个覆盖掉  大概就是这个意思）
			steamId, gameName, gameId, flagId, location, flagSteamId, flagGameName, flagGameId = flagTypeFive(logTxt)
			//开始占领
			break

		}

		gameId, _ := strconv.ParseInt(gameId, 10, 64)
		flagId, _ := strconv.ParseInt(flagId, 10, 64)
		flagGameId, _ := strconv.ParseInt(flagGameId, 10, 64)

		gameName = TruncateStringByCharCount(gameName, 50)

		// 输出字节集（以十六进制表示）
		//fmt.Printf("Byte slice: %x\n", byteSlice)

		// 将字节集转换回字符串
		gameName = filterInvalidChars(gameName)

		flagGameName = TruncateStringByCharCount(flagGameName, 50)

		// 输出字节集（以十六进制表示）
		//fmt.Printf("Byte slice: %x\n", byteSlice)

		// 将字节集转换回字符串
		flagGameName = filterInvalidChars(flagGameName)

		downloadFile := model.DataFlag{FtpInfoId: ftpInfoId, Date: logDate, Time: Time, LogTime: logTime, SteamId: steamId, GameName: gameName, GameId: gameId, FlagId: flagId, FlagGameId: flagGameId, FlagSteamId: flagSteamId, FlagGameName: flagGameName, FlagLocation: flagLocation, EventType: eventType, Location: location}
		dataArr = append(dataArr, downloadFile)
	}
	return dataArr
}

func flagTypeNoe(logTxt string) (string, string, string, string, string) {
	//fmt.Println("原始数据", logTxt)
	//适用于 放置  拆除
	var falgId, steamId, gameName, gameId, location string

	//获取领地旗id
	start := strings.Index(logTxt, "Id: ")

	if start < 0 {
		falgId = ""
	} else {
		end := strings.Index(logTxt[start+4:], ".")
		if end < 0 {
			falgId = ""
		} else {
			falgId = logTxt[start+4 : start+4+end]
		}
	}

	//获取steamId
	start = strings.Index(logTxt, "r: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		steamId = ""
	} else {
		end := strings.Index(logTxt[start+3:], " (")
		if end < 0 {
			steamId = ""
		} else {
			steamId = logTxt[start+3 : start+3+end]
		}
	}

	//获取游戏id
	start = strings.Index(logTxt, "(")
	//fmt.Println("位置1", start)
	if start < 0 {
		gameId = ""
	} else {
		end := strings.Index(logTxt[start+1:], ",")
		if end < 0 {
			gameId = ""
		} else {
			gameId = logTxt[start+1 : start+1+end]
		}
	}

	//获取游戏名
	start = strings.Index(logTxt, ", ")
	//fmt.Println("位置1", start)
	if start < 0 {
		gameName = ""
	} else {
		end := strings.Index(logTxt[start+1:], ")")
		if end < 0 {
			gameName = ""
		} else {
			gameName = logTxt[start+1 : start+1+end]
		}
	}

	start = strings.Index(logTxt, "ion: ")
	if start < 0 {
		location = ""
	} else {
		// 查找第二个单引号的位置
		location = logTxt[start+5:]
	}

	return steamId, gameName, gameId, falgId, location
}

func flagTypeTwo(logTxt string) (string, string, string, string, string, string, string, string) {
	//fmt.Println("原始数据", logTxt)
	//适用于 转让
	var flagId, steamId, gameName, gameId, dataTxt string
	var flagSteamId, flagGameName, flagGameId, location string

	//获取领地旗id
	start := strings.Index(logTxt, "Id: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagId = ""
	} else {
		end := strings.Index(logTxt[start+4:], ".")
		if end < 0 {
			flagId = ""
		} else {
			flagId = logTxt[start+4 : start+4+end]
		}
	}

	start = strings.Index(logTxt, "ion: ")
	if start < 0 {
		location = ""
	} else {
		// 查找第二个单引号的位置
		location = logTxt[start+5:]
	}

	//********************转让人***************************

	start = strings.Index(logTxt, "New")
	//fmt.Println("位置1", start)
	if start < 0 {
		dataTxt = ""
	} else {
		end := strings.Index(logTxt[start+3:], ".")
		if end < 0 {
			dataTxt = ""
		} else {
			dataTxt = logTxt[start+3 : start+3+end]
		}
	}

	//获取steamId
	start = strings.Index(dataTxt, "r: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		steamId = ""
	} else {
		end := strings.Index(dataTxt[start+3:], " (")
		if end < 0 {
			steamId = ""
		} else {
			steamId = dataTxt[start+3 : start+3+end]
		}
	}

	//获取游戏id
	start = strings.Index(dataTxt, "(")
	//fmt.Println("位置1", start)
	if start < 0 {
		gameId = ""
	} else {
		end := strings.Index(dataTxt[start+1:], ",")
		if end < 0 {
			gameId = ""
		} else {
			gameId = dataTxt[start+1 : start+1+end]
		}
	}

	//获取游戏名
	start = strings.Index(dataTxt, ", ")
	//fmt.Println("位置1", start)
	if start < 0 {
		gameName = ""
	} else {
		end := strings.Index(dataTxt[start+2:], ")")
		if end < 0 {
			gameName = ""
		} else {
			gameName = dataTxt[start+2 : start+2+end]
		}
	}

	//********************接接人***************************

	start = strings.Index(logTxt, "Old")
	//fmt.Println("位置1", start)
	if start < 0 {
		dataTxt = ""
	} else {
		end := strings.Index(logTxt[start+3:], ".")
		if end < 0 {
			dataTxt = ""
		} else {
			dataTxt = logTxt[start+3 : start+3+end]
		}
	}

	//获取steamId
	start = strings.Index(dataTxt, "r: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagSteamId = ""
	} else {
		end := strings.Index(dataTxt[start+3:], " (")
		if end < 0 {
			flagSteamId = ""
		} else {
			flagSteamId = dataTxt[start+3 : start+3+end]
		}
	}

	//获取游戏id
	start = strings.Index(dataTxt, "(")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagGameId = ""
	} else {
		end := strings.Index(dataTxt[start+1:], ",")
		if end < 0 {
			flagGameId = ""
		} else {
			flagGameId = dataTxt[start+1 : start+1+end]
		}
	}

	//获取游戏名
	start = strings.Index(dataTxt, ", ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagGameName = ""
	} else {
		end := strings.Index(dataTxt[start+2:], ")")
		if end < 0 {
			flagGameName = ""
		} else {
			flagGameName = dataTxt[start+2 : start+2+end]
		}
	}

	return steamId, gameName, gameId, flagId, location, flagSteamId, flagGameName, flagGameId
}

func flagTypeTree(logTxt string) (string, string, string, string, string, string, string, string, string) {

	//适用于 占领 取消占领
	var flagId, steamId, gameName, gameId, dataTxt string
	var flagSteamId, flagGameName, flagGameId, location, flagLocation string

	//获取领地旗id
	start := strings.Index(logTxt, "Id: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagId = ""
	} else {
		end := strings.Index(logTxt[start+4:], ".")
		if end < 0 {
			flagId = ""
		} else {
			flagId = logTxt[start+4 : start+4+end]
		}
	}

	//********************转让人***************************

	start = strings.Index(logTxt, "Us")
	//fmt.Println("位置1", start)
	if start < 0 {
		dataTxt = ""
	} else {
		end := strings.Index(logTxt[start+2:], ". O")
		if end < 0 {
			dataTxt = ""
		} else {
			dataTxt = logTxt[start+2 : start+2+end]
		}
	}

	//获取steamId
	start = strings.Index(dataTxt, ", ")
	//fmt.Println("位置1", start)
	if start < 0 {
		steamId = ""
	} else {
		end := strings.Index(dataTxt[start+2:], ")")
		if end < 0 {
			steamId = ""
		} else {
			steamId = dataTxt[start+2 : start+2+end]
		}
	}

	//获取游戏id
	start = strings.Index(dataTxt, "(")
	//fmt.Println("位置1", start)
	if start < 0 {
		gameId = ""
	} else {
		end := strings.Index(dataTxt[start+1:], ",")
		if end < 0 {
			gameId = ""
		} else {
			gameId = dataTxt[start+1 : start+1+end]
		}
	}

	//获取游戏名
	start = strings.Index(dataTxt, "r: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		gameName = ""
	} else {
		end := strings.Index(dataTxt[start+3:], " (")
		if end < 0 {
			gameName = ""
		} else {
			gameName = dataTxt[start+3 : start+3+end]
		}
	}

	start = strings.Index(logTxt, "ion: ")
	if start < 0 {
		location = ""
	} else {
		// 查找第二个单引号的位置
		location = logTxt[start+5:]
	}

	//********************接接人***************************

	start = strings.Index(logTxt, "Ow")
	//fmt.Println("位置1", start)
	if start < 0 {
		dataTxt = ""
	} else {
		dataTxt = logTxt[start+3:]
	}

	//获取steamId
	start = strings.Index(dataTxt, ", ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagSteamId = ""
	} else {
		end := strings.Index(dataTxt[start+2:], ")")
		if end < 0 {
			flagSteamId = ""
		} else {
			flagSteamId = dataTxt[start+2 : start+2+end]
		}
	}

	//获取游戏id
	start = strings.Index(dataTxt, "(")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagGameId = ""
	} else {
		end := strings.Index(dataTxt[start+1:], ",")
		if end < 0 {
			flagGameId = ""
		} else {
			flagGameId = dataTxt[start+1 : start+1+end]
		}
	}

	//获取游戏名
	start = strings.Index(dataTxt, "r: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagGameName = ""
	} else {
		end := strings.Index(dataTxt[start+3:], " (")
		if end < 0 {
			flagGameName = ""
		} else {
			flagGameName = dataTxt[start+3 : start+3+end]
		}
	}

	/*start = strings.Index(logTxt, "ion: ")
	starts := strings.Index(logTxt[start+5:], "ion: ")
	if start < 0 {
		flagLocation = ""
	} else {
		// 查找第二个单引号的位置
		flagLocation = logTxt[start+5+starts+5:]
	}*/

	return steamId, gameName, gameId, flagId, location, flagSteamId, flagGameName, flagGameId, flagLocation
}

func flagTypeFour(logTxt string) (string, string, string, string, string, string, string, string, string) {
	//fmt.Println("原始数据", logTxt)
	//适用于 占领 取消占领
	var flagId, steamId, gameName, gameId, dataTxt string
	var flagSteamId, flagGameName, flagGameId, location, flagLocation string

	//获取领地旗id
	start := strings.Index(logTxt, "Id: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagId = ""
	} else {
		end := strings.Index(logTxt[start+4:], ".")
		if end < 0 {
			flagId = ""
		} else {
			flagId = logTxt[start+4 : start+4+end]
		}
	}

	//********************转让人***************************

	start = strings.Index(logTxt, "Us")
	//fmt.Println("位置1", start)
	if start < 0 {
		dataTxt = ""
	} else {
		end := strings.Index(logTxt[start+2:], ". O")
		if end < 0 {
			dataTxt = ""
		} else {
			dataTxt = logTxt[start+2 : start+2+end]
		}
	}

	//获取steamId
	start = strings.Index(dataTxt, ", ")
	//fmt.Println("位置1", start)
	if start < 0 {
		steamId = ""
	} else {
		end := strings.Index(dataTxt[start+2:], ")")
		if end < 0 {
			steamId = ""
		} else {
			steamId = dataTxt[start+2 : start+2+end]
		}
	}

	//获取游戏id
	start = strings.Index(dataTxt, "(")
	//fmt.Println("位置1", start)
	if start < 0 {
		gameId = ""
	} else {
		end := strings.Index(dataTxt[start+1:], ",")
		if end < 0 {
			gameId = ""
		} else {
			gameId = dataTxt[start+1 : start+1+end]
		}
	}

	//获取游戏名
	start = strings.Index(dataTxt, "r: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		gameName = ""
	} else {
		end := strings.Index(dataTxt[start+3:], " (")
		if end < 0 {
			gameName = ""
		} else {
			gameName = dataTxt[start+3 : start+3+end]
		}
	}

	//********************接接人***************************

	start = strings.Index(logTxt, "Ow")
	//fmt.Println("位置1", start)
	if start < 0 {
		dataTxt = ""
	} else {
		dataTxt = logTxt[start+3:]
	}

	//获取steamId
	start = strings.Index(dataTxt, ", ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagSteamId = ""
	} else {
		end := strings.Index(dataTxt[start+2:], ")")
		if end < 0 {
			flagSteamId = ""
		} else {
			flagSteamId = dataTxt[start+2 : start+2+end]
		}
	}

	//获取游戏id
	start = strings.Index(dataTxt, "(")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagGameId = ""
	} else {
		end := strings.Index(dataTxt[start+1:], ",")
		if end < 0 {
			flagGameId = ""
		} else {
			flagGameId = dataTxt[start+1 : start+1+end]
		}
	}

	//获取游戏名
	start = strings.Index(dataTxt, "r: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagGameName = ""
	} else {
		end := strings.Index(dataTxt[start+3:], " (")
		if end < 0 {
			flagGameName = ""
		} else {
			flagGameName = dataTxt[start+3 : start+3+end]
		}
	}

	//获取游戏名
	start = strings.Index(logTxt, "ion: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		location = ""
	} else {
		end := strings.Index(logTxt[start+5:], ". O")
		if end < 0 {
			location = ""
		} else {
			location = logTxt[start+5 : start+5+end]
		}
	}

	start = strings.Index(logTxt, "ion: ")
	starts := strings.Index(logTxt[start+5:], "ion: ")
	if start < 0 {
		flagLocation = ""
	} else {
		// 查找第二个单引号的位置
		flagLocation = logTxt[start+5+starts+5:]
	}

	return steamId, gameName, gameId, flagId, flagSteamId, flagGameName, flagGameId, location, flagLocation
}

func flagTypeFive(logTxt string) (string, string, string, string, string, string, string, string) {
	//fmt.Println("原始数据", logTxt)
	//适用于 转让
	var flagId, steamId, gameName, gameId, dataTxt string
	var flagSteamId, flagGameName, flagGameId, location string

	//获取领地旗id
	start := strings.Index(logTxt, "Id: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagId = ""
	} else {
		end := strings.Index(logTxt[start+4:], ".")
		if end < 0 {
			flagId = ""
		} else {
			flagId = logTxt[start+4 : start+4+end]
		}
	}

	start = strings.Index(logTxt, "ion: ")
	if start < 0 {
		location = ""
	} else {
		// 查找第二个单引号的位置
		location = logTxt[start+5:]
	}

	//********************转让人***************************

	start = strings.Index(logTxt, "Ove")
	//fmt.Println("位置1", start)
	if start < 0 {
		dataTxt = ""
	} else {
		end := strings.Index(logTxt[start+3:], ".")
		if end < 0 {
			dataTxt = ""
		} else {
			dataTxt = logTxt[start+3 : start+3+end]
		}
	}

	//获取steamId
	start = strings.Index(dataTxt, "r: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		steamId = ""
	} else {
		end := strings.Index(dataTxt[start+3:], " (")
		if end < 0 {
			steamId = ""
		} else {
			steamId = dataTxt[start+3 : start+3+end]
		}
	}

	//获取游戏id
	start = strings.Index(dataTxt, "(")
	//fmt.Println("位置1", start)
	if start < 0 {
		gameId = ""
	} else {
		end := strings.Index(dataTxt[start+1:], ",")
		if end < 0 {
			gameId = ""
		} else {
			gameId = dataTxt[start+1 : start+1+end]
		}
	}

	//获取游戏名
	start = strings.Index(dataTxt, ", ")
	//fmt.Println("位置1", start)
	if start < 0 {
		gameName = ""
	} else {
		end := strings.Index(dataTxt[start+2:], ")")
		if end < 0 {
			gameName = ""
		} else {
			gameName = dataTxt[start+2 : start+2+end]
		}
	}

	//********************接接人***************************

	start = strings.Index(logTxt, "Old")
	//fmt.Println("位置1", start)
	if start < 0 {
		dataTxt = ""
	} else {
		end := strings.Index(logTxt[start+3:], ".")
		if end < 0 {
			dataTxt = ""
		} else {
			dataTxt = logTxt[start+3 : start+3+end]
		}
	}

	//获取steamId
	start = strings.Index(dataTxt, "r: ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagSteamId = ""
	} else {
		end := strings.Index(dataTxt[start+3:], " (")
		if end < 0 {
			flagSteamId = ""
		} else {
			flagSteamId = dataTxt[start+3 : start+3+end]
		}
	}

	//获取游戏id
	start = strings.Index(dataTxt, "(")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagGameId = ""
	} else {
		end := strings.Index(dataTxt[start+1:], ",")
		if end < 0 {
			flagGameId = ""
		} else {
			flagGameId = dataTxt[start+1 : start+1+end]
		}
	}

	//获取游戏名
	start = strings.Index(dataTxt, ", ")
	//fmt.Println("位置1", start)
	if start < 0 {
		flagGameName = ""
	} else {
		end := strings.Index(dataTxt[start+2:], ")")
		if end < 0 {
			flagGameName = ""
		} else {
			flagGameName = dataTxt[start+2 : start+2+end]
		}
	}

	return steamId, gameName, gameId, flagId, location, flagSteamId, flagGameName, flagGameId
}
